package edu.ccut.saturn.component.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import edu.ccut.saturn.component.IParameter;

/**
 * {@link IParameter} implementation
 * 
 * @see IParameter
 * @author LeslieGu
 */
public class SaturnParameter implements IParameter {

	private Object value;

	private Class<?> classType;

	public SaturnParameter(Object value, Class<?> classType) {
		this.value = value;
		this.classType = classType;
	}
	
	public SaturnParameter(IParameter parameter) {
		this(parameter.getValue(), parameter.getClassType());
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		if (this.classType != null && this.value != null
				&& !this.classType.isInstance(value)) {

			throw new IllegalArgumentException("need " + this.classType
					+ " type " + value.getClass().getName());
		}

		this.value = value;
	}

	public Class<?> getClassType() {
		return classType;
	}

	public void setClassType(Class<?> classType) {
		this.classType = classType;
	}

	@Override
	public String toString() {
		return "parameter : { value : '" + value + "', type :'"
				+ classType.toString() + "'}";
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		IParameter objectP = new SaturnParameter("", Object.class);
		objectP.setValue("");

		IParameter boolP = new SaturnParameter("", Boolean.class);
		boolP.setValue(false);

		IParameter byteP = new SaturnParameter("", Byte.class);
		byteP.setValue(Byte.parseByte("0"));

		IParameter characterP = new SaturnParameter("", Character.class);
		characterP.setValue(Character.valueOf('0'));

		IParameter decimalP = new SaturnParameter("", BigDecimal.class);
		decimalP.setValue(BigDecimal.valueOf(0));

		IParameter doubleP = new SaturnParameter("", Double.class);
		doubleP.setValue(Double.parseDouble("0"));

		IParameter floatP = new SaturnParameter("", Float.class);
		floatP.setValue(Float.parseFloat("0"));

		IParameter intP = new SaturnParameter("", Integer.class);
		intP.setValue(0);

		IParameter alistP = new SaturnParameter("", ArrayList.class);
		alistP.setValue(new ArrayList());

		IParameter longP = new SaturnParameter("", Long.class);
		longP.setValue(Long.parseLong("0"));

		IParameter shortP = new SaturnParameter("", Short.class);
		shortP.setValue(Short.parseShort("0"));

		IParameter llistP = new SaturnParameter("", LinkedList.class);
		llistP.setValue(new LinkedList());

		IParameter listP = new SaturnParameter("", List.class);
		listP.setValue(new ArrayList());

	}
}
